<?php

namespace App\Repositories;

use App\Models\Admin\OrderBaleModel;
use App\Models\Admin\OrderTempModel;
use App\Models\Main\CompanyContractModel;
use App\Models\User\UserOrdersModel;
use DB;

class OrdersRepository
{

    protected $order_bale_mdl;
    protected $order_temp_mdl;
    protected $user_orders_mdl;
    protected $contract;  //订单合同

    public function __construct()
    {
        $this->order_bale_mdl = new OrderBaleModel();
        $this->order_temp_mdl = new OrderTempModel();
        $this->user_orders_mdl = new UserOrdersModel();
        $this->contract = new CompanyContractModel();
    }

    /**
     * 发布中的订单
     * @param $uid
     * @param $page
     * @param $pageSize
     * @return array
     */
    public function pushingLists($uid,$page,$pageSize)
    {
        $page = $page <= 0 ? 1 : $page;

        $offset = ($page-1)*$pageSize;

        $orders = $this->order_bale_mdl->with('orderTemp')->where('uid',$uid)->where('del',0)->skip($offset)->take($pageSize)->get();

        return $orders->isEmpty() ? [] : $orders->toArray();
    }

    /**
     * 抢单中的订单
     * @param $uid
     * @param $page
     * @param $pageSize
     * @return array
     */
    public function baleLists($uid,$page,$pageSize)
    {
        $page = $page <= 0 ? 1 : $page;

        $offset = ($page-1)*$pageSize;

        $orders = $this->order_temp_mdl->with('orderDetail')->where('fid',$uid)->where('status','!=',4)->skip($offset)->take($pageSize)->get();

        return $orders->isEmpty() ? [] : $orders->toArray();
    }

    /**
     * 生产中的订单
     * @param $uid
     * @param $page
     * @param $pageSize
     * @return array
     */
    public function produceLists($uid,$page,$pageSize)
    {
        $page = $page <= 0 ? 1 : $page;

        $offset = ($page-1)*$pageSize;

        $orders = $this->user_orders_mdl->with('orderFactory')->where('fid',$uid)->skip($offset)->take($pageSize)->get();

        return $orders->isEmpty() ? [] : $orders->toArray();
    }

    /**
     * 已完成的订单
     * @param $uid
     * @param $page
     * @param $pageSize
     * @return array
     */
    public function overLists($uid,$page,$pageSize)
    {
        $page = $page <= 0 ? 1 : $page;

        $offset = ($page-1)*$pageSize;

        $orders = $this->user_orders_mdl->with('orderFactory')->where('fid',$uid)->where('status',9)->skip($offset)->take($pageSize)->get();

        return $orders->isEmpty() ? [] : $orders->toArray();
    }

    /**
     * 根据条件获取订单
     * @param $uid
     * @param $page
     * @param $pageSize
     * @return array
     */
    public function getOrders($where=[],$page=1,$pageSize=20)
    {
        $offset = ($page-1)*$pageSize;
        $orders = $this->order_bale_mdl->where($where)->skip($offset)->take($pageSize)->get();

        return $orders->isEmpty() ? [] : $orders->toArray();
    }

    /**
     * 原料面料获取
     */
    public function getFabric($orderId)
    {
        $model = $this->user_orders_mdl->where('id',$orderId)->first();
        return ($model && $model->yuanliao) ?
            json_decode($model->yuanliao) : array();
    }

    /**
     * 原料面料设置
     */
    public function setFabric($orderId,$data)
    {
        $yuanliao = json_encode($data,JSON_UNESCAPED_UNICODE);
        return $this->user_orders_mdl->where('id',$orderId)
            ->update(['yuanliao'=>$yuanliao, 'updated_at'=>time()]);
    }

    /**
     * 订单合同获取
     */
    public function getContract($baleId)
    {
        return $this->contract->where('id',$baleId)->first();
    }

    /**
     * 订单合同设置
     */
    public function setContract($baleId,$data)
    {
        return $this->contract->where('id',$baleId)->update($data);
    }
}